﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;
using BLL;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using POS.Framework;

namespace POS.ModForm
{
    public partial class frmUserPosSetUp : DevExpress.XtraEditors.XtraForm
    {
        Session s;
        public frmUserPosSetUp()
        {
            InitializeComponent();
        }

        private void frmUserPosSetUp_Load(object sender, EventArgs e)
        {
            popFormButtonUC.OK.Click+=OK_Click;
            this.BindData();
        }
        private void OK_Click(object sender, EventArgs e)
        {
            string name = xtraTabControl.SelectedTabPage.Name;
            if (name == "xtraTabPageBill") 
            {
                MemberInfo.UserPosSetUp.StoreSaleDetailMerge = this._StoreSaleDetailMerge.Checked;
                MemberInfo.UserPosSetUp.DeliverySaleDetailMerge = this._DeliverySaleDetailMerge.Checked;
                if (this._StoreSaleWarehouse.SelectedIndex >= 0) 
                {
                    string whId=dicWarehouse.First(kv => kv.Value == this._StoreSaleWarehouse.Text).Key;
                    MemberInfo.UserPosSetUp.StoreSaleWarehouse = s.GetObjectByKey<B_Warehouse>(whId);
                }
                if (this._DeliverySaleWarehouse.SelectedIndex >= 0)
                {
                    string whId = dicWarehouse.First(kv => kv.Value == this._DeliverySaleWarehouse.Text).Key;
                    MemberInfo.UserPosSetUp.DeliverySaleWarehouse = s.GetObjectByKey<B_Warehouse>(whId);
                }
                MemberInfo.UserPosSetUp.Save();
                MsgBox.ShowTips("用户配置信息保存成功！");
            }
        }
        Dictionary<string, string> dicWarehouse = new Dictionary<string, string>();
        void BindData() 
        {
            this._StoreSaleDetailMerge.Checked = MemberInfo.UserPosSetUp.StoreSaleDetailMerge;
            this._DeliverySaleDetailMerge.Checked = MemberInfo.UserPosSetUp.DeliverySaleDetailMerge;
            s = DatabaseSchema.Session;
            XPCollection<B_Warehouse> warehouses = new XPCollection<B_Warehouse>(s, CriteriaOperator.Parse("StoreID.StoreID=?", MemberInfo.Store.StoreID));
            foreach (B_Warehouse w in warehouses)
            {
                _StoreSaleWarehouse.Properties.Items.Add(w.WarehouseName);
                _DeliverySaleWarehouse.Properties.Items.Add(w.WarehouseName);
                dicWarehouse.Add(w.WarehouseID,w.WarehouseName);
            }
            if (MemberInfo.UserPosSetUp.StoreSaleWarehouse != null) 
            {
                this._StoreSaleWarehouse.Text = dicWarehouse[MemberInfo.UserPosSetUp.StoreSaleWarehouse.WarehouseID];
            }
            if (MemberInfo.UserPosSetUp.DeliverySaleWarehouse != null)
            {
                this._DeliverySaleWarehouse.Text = dicWarehouse[MemberInfo.UserPosSetUp.DeliverySaleWarehouse.WarehouseID];
            }
        }
    }
}